package com.pengtu.gsj.controller.system;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.BaseController;
import com.pengtu.gsj.entity.app.Role;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.exception.ControllerException;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.system.RoleService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * 
 * RoleController
 * 
 * 2017年4月12日 下午3:00:48
 * @author zl
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	
	
	/**
	 * 
	 * loadEntity:
	 * 适用: 每个方法执行之前都会执行该方法
	 * @param id  role主键id
	 * @return 返回持久对象role或者新创建对象
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public void loadEntity(String id, Model model){
		Role role ;
		if (StringUtils.isNotEmpty(id)) {
			role = roleService.getRole(id);
		}else {
			role = new Role();
		}
		model.addAttribute("role", role);
		model.addAttribute("top", SpringMvcUtils.getParameter("top"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
	}
	
	/**
	 * 
	 * getRoleList:
	 * 适用:显示所有角色列表
	 * @param model 传递参数
	 * @return 返回到角色列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("list")
	public String getRoleList(Model model){
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		List<Role> list = roleService.getAllRole();
		model.addAttribute("list", list);
		return "system/role";
	}
	
	/**
	 * 
	 * showOrInputRoleInfo:
	 * 适用:新增和查看角色详情时会用到该方法
	 * @param id   角色主键id
	 * @param model  传递参数
	 * @return    返回角色详情页面
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("input")
	@Token(save = true)
	public String showOrInputRoleInfo(@ModelAttribute Role role,Model model){
		List<Sysmenu> menuList =sysmenuManagerService.getAll();
		model.addAttribute("menuList", menuList);
		return "system/role_input";
	}
	
	/**
	 * 
	 * saveRole:
	 * 适用:新增和修改角色信息时
	 * @param role   接受角色属性信息对象
	 * @param model  传递对象
	 * @return  角色列表页面
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String saveRole(@ModelAttribute Role role,RedirectAttributes attributes)throws Exception{
		String oldRoleName  = SpringMvcUtils.getParameter("oldName");
		if (!roleService.isRoleNameUnique(role.getRoleName(), oldRoleName)) {
			attributes.addFlashAttribute("msg", "角色["+role.getRoleName()+"]保存失败，角色名已存在...");
		}else {
			roleService.saveRole(role);
			attributes.addFlashAttribute("msg", "角色["+role.getRoleName()+"]保存成功！");
		}
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/role/list.do";
	}
	
	
	/**
	 * 
	 * deleteRole:
	 * 适用:页面删除某个角色时
	 * @param id     角色主键id
	 * @param model  参数传递工具
	 * @return 返回到角色列表页面
	 * @throws Exception 异常统一处理
	 * @exception 抛出参数异常错误并跳转到相应页面
	 * @since  1.0.0
	 */
	@RequestMapping("delete")
	public String deleteRole(String id,RedirectAttributes attributes){
		if (StringUtils.isEmpty(id)) {
			throw new ControllerException("参数id错误");
		}
		roleService.deleteRole(id);
		attributes.addFlashAttribute("msg", "角色删除成功!");
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/role/list.do";
	}
	
	/**
	 * 
	 * checkRoleName:
	 * 适用:在新增和修改角色信息时，判断输入的角色名称是否已经存在了
	 * @return 返回 true或者false json字符串
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="check",produces = "application/json; charset=utf-8")
	public Boolean checkRoleName(){
		String oldName = SpringMvcUtils.getParameter("oldName");
		String roleName = SpringMvcUtils.getParameter("roleName");
		Boolean exist = roleService.isRoleNameUnique(roleName, oldName);
		return exist;
	}
	
}
